﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common;
using SYH.Config;
using System.Data.SqlClient;

namespace SYH.System.DAL
{
    public partial class SystemRole
    {
        public SystemRole()
        { }
        #region  基本方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SystemRole");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;
            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH.System.Model.SystemRole model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SystemRole(");
            strSql.Append("RoleName,ActionCollection,Status)");
            strSql.Append(" values (");
            strSql.Append("@RoleName,@ActionCollection,@Status)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleName", SqlDbType.NVarChar,20),
					new SqlParameter("@ActionCollection", SqlDbType.VarChar,5000),
					new SqlParameter("@Status", SqlDbType.Int,4)};
            parameters[0].Value = model.RoleName;
            parameters[1].Value = model.ActionCollection;
            parameters[2].Value = model.Status;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH.System.Model.SystemRole model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SystemRole set ");
            strSql.Append("RoleName=@RoleName,");
            strSql.Append("ActionCollection=@ActionCollection,");
            strSql.Append("Status=@Status");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleName", SqlDbType.NVarChar,20),
					new SqlParameter("@ActionCollection", SqlDbType.VarChar,5000),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.RoleName;
            parameters[1].Value = model.ActionCollection;
            parameters[2].Value = model.Status;
            parameters[3].Value = model.Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update   SystemRole  set Status=-1");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  SystemRole  set Status=-1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return rows > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.System.Model.SystemRole GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,RoleName,ActionCollection,Status from SystemRole ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH.System.Model.SystemRole model = new SYH.System.Model.SystemRole();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.System.Model.SystemRole DataRowToModel(DataRow row)
        {
            SYH.System.Model.SystemRole model = new SYH.System.Model.SystemRole();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["RoleName"] != null)
                {
                    model.RoleName = row["RoleName"].ToString();
                }
                if (row["ActionCollection"] != null)
                {
                    model.ActionCollection = row["ActionCollection"].ToString();
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,RoleName,ActionCollection,Status ");
            strSql.Append(" FROM SystemRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,RoleName,ActionCollection,Status ");
            strSql.Append(" FROM SystemRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SystemRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return Common.TypeParse.ObjectToInt(obj, 0);
        }

        #endregion  BasicMethod

    }
}
